<?php declare(strict_types=1);

namespace App\Api\V2\Controller\Redirect;


use App\Api\Controllers\BaseController;
use App\Services\Notify\AliYunNotifyService;
use App\Services\Redirect\AliyunRedirectService;
use App\Traits\ResponseTrait;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class AliyunController extends BaseController
{

    use ResponseTrait;


//http://example.com/doc/toAuthPage.html?app_id=2021000000000318&source=alipay_app_auth&application_type=TINYAPP,WEBAPP&app_auth_code=P1798b23682e34d96859afa000000003
    public function sourceCode(Request $request): string
    {
        try {
            $data = $request->all();
            Log::channel("redirect")->info("请求参数：" . date("Y-m-d H:i:s", time()), [
                "data" => $data
            ]);

            //在这里写逻辑
            $source = $data["source"] ?? "";
            if (empty($source)) {
                throw new \Exception("source 不存在");
            }
            switch ($source) {
                case "alipay_app_auth":
                    $service = new AliyunRedirectService();
                    //{"app_id":"2021004167603274","source":"alipay_app_auth","state":"STORE_ID_VALUE","app_auth_code":"P821ebc0f499c4be496ea2978af92164"}
                    $result = $service->alipayAppAuth($data);
                    break;
            }
            return $result;
        } catch (\Exception $e) {
            $msg = $e->getMessage() . $e->getFile() . $e->getLine();
            Log::channel("redirect")->error("Error:" . date("Y-m-d H:i:s", time()) . "__" . $msg);
            return $this->errorResponse($msg);
        }
    }

}